package com.bp.hearthealth.pulse.lnapps.bp_view

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
 * @author isnoam
 * @package_name SpacingItemDecoration
 * @description：第一个item，margin顶部的距离，最后一个item，margin底部的距离
 */
class BPSpacingItemDecoration(
    private val bottomSpacing: Int, private val marginTop: Int, vararg val marginFirstTop: Int
) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
    ) {
        val position = parent.getChildAdapterPosition(view)
        val totalItemCount = parent.adapter?.itemCount ?: 0
        when (position) {
            0 -> {
                outRect.top = marginTop
            }

            1 -> {
                if (marginFirstTop.isNotEmpty()) {
                    outRect.top = marginFirstTop[0]
                }
                if (totalItemCount==2) {
                    outRect.bottom = bottomSpacing
                }
            }

            else -> {
                if (totalItemCount != 1 && totalItemCount - 1 == position) {
                    outRect.bottom = bottomSpacing
                }
            }
        }
    }
}
